package top.fengyulou.xye.service;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.fengyulou.common.Jr;
import top.fengyulou.common.UserUtils;
import top.fengyulou.xye.dao.UserRepository;
import top.fengyulou.xye.entity.User;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    /**
     * 用户登录
     *
     * @param user
     * @return
     */
    public Jr login(User user) {
        User byLoginName = userRepository.findByLoginName(user.getLoginName());
        if (ObjectUtils.isNotEmpty(byLoginName)) {
            String password = byLoginName.getPassword();
            String pwd = UserUtils.getPwd(user.getPassword(), byLoginName.getRandomStr());
            if (password.equals(pwd)) {
                return Jr.yes(byLoginName);
            }
        }
        return Jr.no();
    }
}
